//index.js
var history = require('../../utils/history_search.js')
var dateExtension = require("../../utils/date-extension.js");
//获取应用实例
var app = getApp()
Page({
  data: {
    searchQuery:{
      "departureCode": "",
      "arriveCode": "",
      "isChanges": 0,
      "travelDate": ""
    },
    userInfo: {},
    history_scroll:[
      {
        start: "德令哈",
        end: "西宁",
      }, {
        start: "湖北",
        end: "深圳",
      }, {
        start: "深圳",
        end: "西宁",
      }, {
        start: "广东",
        end: "上海",
      }, {
        start: "广东",
        end: "佛山",
      }, {
        start: "佛山",
        end: "汕头",
      }
    ]
  },chooseDate: function (e) {
    var travelDate = this.data.searchQuery.travelDate;
     wx.navigateTo({
       url: '../date/date?selectDate=' + travelDate
    })
  },
  //事件处理函数
  bindViewTap: function() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  onLoad: function () {

    var that = this;
    //调用应用实例的方法获取全局数据
    app.getUserInfo(function(userInfo){
      //更新数据
      that.setData({
        userInfo:userInfo
      })
    })
  },
  onShow: function () {
    this.init_data();
  },
   tapName: function (e) {
      
    var departureCode = this.data.searchQuery.departureCode;
    var arriveCode = this.data.searchQuery.arriveCode;
    var travelDate = this.data.searchQuery.travelDate;
    var isChanges = this.data.searchQuery.isChanges;

    if (!departureCode){
      wx.showToast({
        title: '请先选择始发站',
        icon: 'succes',
        duration: 1000,
        mask: true
      });
      return;
    }
    if (!arriveCode) {
      wx.showToast({
        title: '请先选择到达站',
        icon: 'succes',
        duration: 1000,
        mask: true
      });
      return;
    }
    
    if (!travelDate) {
      wx.showToast({
        title: '请先选择旅行日期',
        icon: 'succes',
        duration: 1000,
        mask: true
      });
      return;
    }
    var redirectParams = '?departureCode=' + departureCode + '&arriveCode=' + arriveCode
      + '&travelDate=' + travelDate + '&isChanges=' + isChanges;
    wx.navigateTo({
      url: '../search/search_ticket' + redirectParams
    })
  }, placeName: function (e) {
    var type = e.currentTarget.dataset.type;
    wx.navigateTo({
      url: '../chooseplace/chooseplace?type=' + type
    })
  },
  init_data:function(){
    var departureCode = wx.getStorageSync('departureCode');
    var arriveCode = wx.getStorageSync('arriveCode');
    var dateValue = wx.getStorageSync('dateValue');
    if (!dateValue){
      var date=new Date();
      dateValue=date.pattern("yyyy-MM-dd");
      wx.setStorageSync('dateValue', dateValue);
    }else{
      var theDate = new Date();
      var formartDate = theDate.pattern("yyyy-MM-dd");
      var bt = dateExtension.chaBetween(formartDate,dateValue);
      if (bt < 0) {
        dateValue = formartDate;
        wx.setStorageSync('dateValue', formartDate);
      }
      
    }
    if (!departureCode){
      departureCode='西宁';
    }
    if (!arriveCode) {
      arriveCode = '西宁';
    }
    var history_scroll = wx.getStorageSync('history_scroll');

    this.setData({
      searchQuery:{
        'departureCode': departureCode,
        'arriveCode': arriveCode,
        'travelDate': dateValue,
        'isChanges': 0,
        'history_scroll': history_scroll
      }
    });
  }, turnLocation:function(){
    if (wx.getStorageSync('departureCode') == this.data.searchQuery.departureCode){
      this.setData({
        searchQuery: { departureCode: wx.getStorageSync('arriveCode'), arriveCode: wx.getStorageSync('departureCode'), travelDate: wx.getStorageSync('dateValue'), 'isChanges': 0, history_scroll: wx.getStorageSync('history_scroll') }
      });
    }else{      
      this.setData({
        searchQuery: { departureCode: wx.getStorageSync('departureCode'), arriveCode: wx.getStorageSync('arriveCode'), travelDate: wx.getStorageSync('dateValue'), 'isChanges': 0, history_scroll: wx.getStorageSync('history_scroll') }
      });
    }
    
  }
})
